keep = ls()

##################
#Load Census Data#
##################

censusTable = fread('../county_elections/cleaned/census_data_wide.csv')
censusTable = censusTable[target_state %in% 'IOWA']
censusTable[, county_u := toupper(target_name) %>% gsub('\\.', '', .)]

#Count white males
age_cols = c('wm1014_1860',
             'wm1519_1860',
             'wm2029_1860',
             'wm3039_1860',
             'wm4049_1860',
             'wm5059_1860',
             'wm6069_1860',
             'wm7079_1860',
             'wm8089_1860',
             'wm9099_1860')

censusTable[, total_whitemen := rowSums(.SD, na.rm = T), .SDcols = age_cols]
censusTable = censusTable[, list(county_u = target_name, mil_age_male_1860, total_whitemen)]
censusTable[county_u=="BUNCOMBE", county_u := "LYON"] 

###########################
##Load Iowa Referenda data#
###########################

referenda.data = read.dta("../suffrage_referenda/raw/ICPSR_04284/DS0001/04284-0001-Data.dta", convert.factors=T) %>% as.data.table
referenda.data[, COID := as.character(COID)]

#Calculate estimated eligible voters: Referenda 1857
referenda.data[, EEV57 := EEV57A]
referenda.data[is.na(EEV57A), EEV57 := EEV57O]

#Calculate estimated eligible voters: Governor 1857
referenda.data[, EEV57G := EEV57O]
referenda.data[is.na(EEV57O), EEV57G := EEV57A]

#Append census to referenda data
setkey(censusTable, county_u)
referenda.data = cbind(referenda.data, censusTable[, .SD, .SDcols =  c('mil_age_male_1860', 'total_whitemen')])

#Create voteshare, military aged male data
iowa_out = referenda.data[, list(county_u = toupper(COID), state = "IOWA",
                     mil_age = mil_age_male_1860,
                     total_whitemen,
                     suff57_yes_count = Y57BS,
                     suff57_no_count = NB57BS + NRO57BS,
                     suff57_denom_count = EEV57,
                     gov57_r_count = R57G,
                     gov57_d_count= A57G + D57G,
                     gov57_denom_count = EEV57G,
                     suff65_yes_count = Y68BS,
                     suff65_no_count = N68BS + RO68BS,
                     suff65_denom_count = EEV68,
                     gov65_r_count = R65G,
                     gov65_d_count= UASD65G,
                     gov65_denom_count = EEV65
                     ),]


#Append enlistment data
ia_veterans = fread("../suffrage_referenda/cleaned/ia_cty_enlistment_cleaned.csv")
ia_veterans[, state := "IOWA"]
ia_veterans[, county_u := County]
ia_veterans[, County := NULL]

setkey(ia_veterans, state, county_u)
setkey(iowa_out, state, county_u)
iowa_out = ia_veterans[iowa_out]

#Save file
fwrite(iowa_out, "./cleaned/ia_referenda_use.csv")

#Cleanup
rm(list = setdiff(ls(), c(keep, 'keep')))
gc()